﻿/***************************************************************************
//* Foxit Software Inc.
//* Copyright(C) 2005-2012, all rights reserved.
//*
//* The following code is copyrighted and contains proprietary information
//* and trade secrets of Foxit Software Inc.                                                      *
***************************************************************************/

/**
* 缩略图显示
*/

define(function (require, exports, module) {

    FXREADER.PDFView.STANDARD_THUMB_WIDTH = 128;
    FXREADER.PDFView.STANDARD_THUMB_HEIGHT = 149;
    FXREADER.PDFView.STANDARD_THUMB_CONTAINER_WIDTH = 138;
    FXREADER.PDFView.STANDARD_THUMB_CONTAINER_HEIGHT = 179;


    FXREADER.PDFView.CPDF_PageThumb = function (pPDFPage, pThumbsView, thumbViewContainerID) {
        if (pPDFPage == null)
            throw new Error("pPDFPage is null!");
        var _pPDFPage = pPDFPage,
             _ThumbImgOffsetTop = 0,
              _ThumbWidth = 0, _ThumbHeight = 0, _bContentCreated = false,
              _pThumbsView = pThumbsView;

        var _self = this;
        var nPageIndex = _pPDFPage.GetPageIndex();
        var _ThumbContainerID = pThumbsView.GetThumbContainerID(nPageIndex);
        //背景图;
        var _ThumbBGImgID = pThumbsView.GetThumbBGImgID(nPageIndex);
        var _WrapThumbID = pThumbsView.GetWrapThumbID(nPageIndex);
        var _ThumbLoaderImgID = pThumbsView.GetThumbLoaderImgID(nPageIndex);
        var _ThumbPageNumberID = pThumbsView.GetThumbNumberID(nPageIndex);
        //页面大小是否更新过,当改变放大倍数时重置;
        var _bScaleUpdated = false;
        //页面是否已经Load完成;
        var _bThumbLoaded = false;
        var _bLoadError = false;
        this.GetPageIndex = function () {
            return pPDFPage.GetPageIndex();
        };
        this.GetThumbContainerID = function () {
            return _ThumbContainerID;
        };
        this.IsLoadError = function () {
            return _bLoadError;
        };
        this.SetThumbImgOffsetTop = function (offsetTop) {
            _ThumbImgOffsetTop = offsetTop;
        };
        this.GetThumbImgOffsetTop = function ()
        {
            return _ThumbImgOffsetTop;
        };
        this.GetPageBGImgID = function () {
            return _ThumbBGImgID;
        };
        this.GetWrapThumbID = function () {
            return _WrapThumbID;
        };
        this.GetThumbLoaderImgID = function () {
            return _ThumbLoaderImgID;
        };

        this.GetThumbPageNumberID = function () {
            return _ThumbPageNumberID;
        };

        this.GetThumbWidth = function () {
            return _ThumbWidth;
        };
        this.SetThumbWidth = function (width) {
            if (isNaN(width))
                return;
            _ThumbWidth = width;
        };

        this.GetThumbsView = function()
        {
        	return _pThumbsView;
        };
        	
        this.GetThumbHeight = function () {
            return _ThumbHeight;
        };
        this.SetThumbHeight = function (height) {
            if (isNaN(height))
                return;
            _ThumbHeight = height;
        };

        this.GetPDFPage = function () {
            return _pPDFPage;
        };
        this.IsContentCreated = function () {
            return _bContentCreated;
        };
        //设置page对应的div是否创建的状态;
        this.SetContentCreatedState = function (bCreated) {
            _bContentCreated = bCreated;
        };
        this.IsScaleUpdated = function () {
            return _bScaleUpdated;
        };
        this.SetScaleUpdated = function (bUpdated) {
            _bScaleUpdated = bUpdated;
        };
        this.IsThumb = function () {
            return true;
        };

        this.GetScale = function () {
            return 1;
        };

        //显示页面;
        this.Show = function () {
            $("#" + _WrapThumbID).addClass("fwr_hidden");
            $("#" + _ThumbBGImgID).removeClass("fwr_hidden");
            $("#" + _ThumbLoaderImgID).hide();
        };

        this.ShowErrorPage = function (imgurl) {

            var jqWrap = $("#" + _WrapThumbID);
            jqWrap.html('');
            jqWrap.removeClass("fwr_hidden");
            $("#" + _ThumbBGImgID).addClass("fwr_hidden");
            $("#" + _ThumbLoaderImgID).hide();

            var thumbWidth = jqWrap.width();
            var thumbHeight = jqWrap.height();

            var fZoomX = FXREADER.Config.errPageImgWidth / thumbWidth;
            var fZoomY = FXREADER.Config.errPageImgHeight / thumbHeight;
            var fZoom = fZoomX >= fZoomY ? fZoomX : fZoomY;
            fZoom = fZoom > 1 ? fZoom : 1;
            var fRealWidth = FXREADER.Config.errPageImgWidth / fZoom;
            var fRealHeight = FXREADER.Config.errPageImgHeight / fZoom;
            var marginLeft = (thumbWidth - fRealWidth) / 2;
            var marginTop = (thumbHeight - fRealHeight) / 2;

            var html = "<div style='position:absolute;width:" + fRealWidth + "px;height:" + fRealHeight + "px;margin-left:" + marginLeft + "px;margin-top:" + marginTop + "px'><img alt='' class='thumbimage' src='" + imgurl + "'  style='width:100%;height:100%' page-index='" + _self.GetPageIndex() + "'></div>";
            jqWrap.append(html);
            _bLoadError = true;

        };

        this.IsPageLoaded = function () {
            return _bThumbLoaded;
        };
        this.SetPageLoaded = function (bLoad) {
            _bThumbLoaded = bLoad;
        };

    }

    return FXREADER.PDFView.CPDF_PageThumb;

});